#include "dircmd2.h"
#include "dirStack.h"
#define RET_SUCCESS "success"
#define RET_FAIL "fail"

int myMkdir(int netfd, dirStack_t *pStack, char *args)
{
    char buf[512] = {0};
    char path[1024] = {0};
    visitStack(pStack, buf);
    printf("mkdir buf = %s\n", buf);
    sprintf(path,"%s/%s", buf, args);

    printf("path = %s\n", path);

    //创建文件夹
    mode_t mode=0777;
    int ret= mkdir(path,mode);

    //返回消息
    train_t train_sendinfo;
    bzero(&train_sendinfo,sizeof(train_t));

    if(ret!=0)
    {
        /* char *fail = "mkdir fail!"; */
        train_sendinfo.length = strlen(RET_FAIL);
        memcpy(train_sendinfo.data, RET_FAIL, train_sendinfo.length);
        send_msg(netfd, &train_sendinfo);
    }
    else {
        /* char *succeed = "mkdir succeed!"; */
        train_sendinfo.length = strlen(RET_SUCCESS);
        memcpy(train_sendinfo.data, RET_SUCCESS, train_sendinfo.length);
        send_msg(netfd, &train_sendinfo);
    }

    return 0;
}

int myRmdir(int netfd, dirStack_t *pStack, char *args)
{
    char buf[512] = {0};
    char path[1024] = {0};
    visitStack(pStack, buf);
    printf("mkdir buf = %s\n", buf);
    sprintf(path,"%s/%s", buf, args);

    //删除文件夹
    int ret = rmdir(path);

    //返回消息
    train_t train_sendinfo;
    bzero(&train_sendinfo,sizeof(train_t));

    if(ret!=0)
    {
        char fail[] = "rmdir fail!";
        train_sendinfo.length = strlen(fail);
        memcpy(train_sendinfo.data, fail, train_sendinfo.length);
        send_msg(netfd, &train_sendinfo);
    }
    else {
        char succeed[] = "rmdir succeed!";
        train_sendinfo.length = strlen(succeed);
        memcpy(train_sendinfo.data, succeed, train_sendinfo.length);
        send_msg(netfd, &train_sendinfo);
    }

    return 0;
}

int myRm(int netfd, dirStack_t *pStack, char *args)
{
    char buf[512] = {0};
    char path[1024] = {0};
    visitStack(pStack, buf);
    printf("mkdir buf = %s\n", buf);
    sprintf(path,"%s/%s", buf, args);

    //删除文件
    int ret = unlink(path);

    //返回消息
    train_t train_sendinfo;
    bzero(&train_sendinfo,sizeof(train_t));

    if(ret!=0)
    {
        char fail[] = "rmfile fail!";
        train_sendinfo.length = strlen(fail);
        memcpy(train_sendinfo.data, fail, train_sendinfo.length);
        send_msg(netfd, &train_sendinfo);
    }
    else {
        char succeed[] = "rmfile succeed!";
        train_sendinfo.length = strlen(succeed);
        memcpy(train_sendinfo.data, succeed, train_sendinfo.length);
        send_msg(netfd, &train_sendinfo);
    }

    return 0;
}

